package com.yidu.boss.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.Seat;
import com.yidu.boss.sys.service.SeatService;
import com.yidu.boss.util.RandomString;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: fund_management_system
 * @ClassName SeatController
 * @author: Zc
 * @create: 2019-11-28 11:01
 **/

@Controller
@RequestMapping("/seat/")
public class SeatController {

    @Resource
    SeatService seatService;

    @RequestMapping("seat")
    public String Seat(){
        return "seat";
    }


    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(Integer page, Integer limit){
        //页面大小，页面
        PageHelper.startPage(page,limit);
        //根据条件查询管理人数据
        List<Seat> seatList=seatService.selectAll();
        //使用pageInfo包装查询后的结果，封装了详细的查询数据，其中参数是列表数据
        PageInfo<Seat> custodianPageInfo =new PageInfo<Seat>(seatList);
        //列表数据
        List<Seat> custodianData =custodianPageInfo.getList();
        //map集合
        Map<String,Object> map =new HashMap<String,Object>();
        //分页查询编码从0开始
        map.put("code", 0);
        //信息
        map.put("msg", "");
        //分页统计数量
        map.put("count",custodianPageInfo.getTotal());
        //分页数据
        map.put("data", custodianData);
        return map;
    }


    @RequestMapping("delete")
    @ResponseBody
    public String delete(String[] ids){
        int i=0;
        for (String id : ids) {
            i += seatService.delete(id);
        }
        if (i>0){
            return "删除成功";
        }
        return "删除失败";

    }

    @RequestMapping("add")
    @ResponseBody
    public String add(Seat Seat){
        int i = seatService.add(Seat);

        if (i>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @RequestMapping("updata")
    @ResponseBody
    public String updata(Seat Seat){
        int i = seatService.update(Seat);
        if (i>0){
            return "修改成功";
        }
        return "修改失败";
    }

    /**
     * 模糊查询
     *
     * @param condition 模糊查询条件
     * @param page      页码
     * @param limit     数据条数
     * @return 返回前端数据格式
     */
    @RequestMapping("selectLike")
    @ResponseBody
    public Object selectLike(String condition, Integer page, Integer limit) {
        System.out.println(condition);
        //判断模糊查询条件参数是否为空
        if(condition.equals("")){
            //为空显示所有列表页面
            Object o =selectAll(page,limit);
            return o;
        }
        //开始页
        PageHelper.startPage(page,limit);
        //调用业务层方法
        List<Seat> seatList =seatService.selectLike(condition);
        PageInfo<Seat> seatPageInfo =new PageInfo<Seat>(seatList);
        //调用工具类的分页计数数据格式：map集合
        Map<String, Object> map = RandomString.pagingALG(seatPageInfo,seatPageInfo.getTotal());
        return map;
    }

}
